
package com.lt.test.provider.dao.impl;

import com.lt.test.provider.dao.BaseDaoSupport;
import com.lt.test.provider.dao.KtShopLossprofitItemDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class KtShopLossprofitItemDaoImpl extends BaseDaoSupport implements KtShopLossprofitItemDao {
	
	private static final Logger log = Logger.getLogger(KtShopLossprofitItemDaoImpl.class); 

	/**
	 * 根据查询条件获取实现集
	 */
	public List query(Map parameter) {
		List list = new ArrayList();
		try {  
			list = this.queryForList("queryKtShopLossprofitItems",parameter);  
		} catch (Exception e) {
			log.error("错误:");
			e.printStackTrace();
		}
	    return list;
	}
	

	/**
	 * 创建实体
	 */
    public int create(Map parameter)
    {  
    	return this.insert("createKtShopLossprofitItem", parameter);
    }
    
    /**
     * 更新实体
     */
    public int update(Map parameter)
    {
    	return this.update("updateKtShopLossprofitItem",parameter);
    }
     
    /**
     * 保存实体
     */
    public int save(Map parameter)
    {
    	return this.update("saveKtShopLossprofitItem",parameter);
    } 
    
     
     /**
     * 删除实体
     */
    public int delete(java.lang.String id)
    {
    	return this.delete("deleteKtShopLossprofitItem",id);
    }
    /**
	 * 根据主键获取实体信息
	 */
	public Map findById(java.lang.String id) {
		return this.queryForSingle("queryKtShopLossprofitItem", id);
	}
  	
  	/**
     * 批量插入
     */
	public int batch(List values){
  		int iEffectedRow = 0;
  		iEffectedRow = this.insert("batchKtShopLossprofitItem",values); 
  		return iEffectedRow;
	}
  	
  	/**
     * 批量更新
     */
  	public int batchUpdate(List values){
  		int iEffectedRow = 0;
  		iEffectedRow = this.update("batchUpdateKtShopLossprofitItem",values);
  		return iEffectedRow;
	}
  	
  	/**
     * 批量删除
     */
  	public int batchDelete(List values){
  		int iEffectedRow = 0;
  		iEffectedRow = this.delete("batchDeleteKtShopLossprofitItem",values);
  		return iEffectedRow;
	}
  	
  	@Override
  	public void deleteItems(String id) {
  		// TODO Auto-generated method stub
  		StringBuilder sqlStr = new StringBuilder();
  		sqlStr.append("DELETE FROM KT_SHOP_LOSSPROFIT_ITEM WHERE ORDER_ID = ?");
  		this.excuteSql(sqlStr.toString(), new Object[]{id});
  	}
  	
  	@Override
  	public List<Map<String, Object>> getItemsByOrderId(String orderId) {
  		// TODO Auto-generated method stub
  		StringBuilder sqlStr = new StringBuilder();
  		sqlStr.append("SELECT t1.*,t2.USERNAME USER_NAME,t3.USERNAME MODIFY_USER_NAME,t4.PRODUCT_NAME FROM KT_SHOP_LOSSPROFIT_ITEM t1 FULL JOIN PLATFORM_USER t2 ON t1.CREATE_USER=t2.ID FULL JOIN PLATFORM_USER t3 ON t1.MODIFY_USER=t3.ID FULL JOIN D_PROD t4 ON t1.PRODUCT_CODE=t4.PRODUCT_CODE WHERE 1=1 AND t1.ID IS NOT NULL AND ORDER_ID= ?");
  		return this.queryAsArray(sqlStr.toString(), new Object[]{orderId});
  	}
  	@Override
  	public void batchUpdateItems(List list) {
  		// TODO Auto-generated method stub
  		StringBuilder sqlStr = new StringBuilder();
  		sqlStr.append("UPDATE KT_SHOP_LOSSPROFIT_ITEM SET STATE = ?,MODIFY_TIME = to_date(?,'yyyy-mm-dd hh24:mi:ss') ,MODIFY_USER = ? WHERE ORDER_ID = ? AND PRODUCT_CODE = ?");
  		this.batchInsert(sqlStr.toString(), list);
  	}
  	
  	@Override
  	public List<Map<String, Object>> getUnFinishItems(String orderId, String state) {
  		// TODO Auto-generated method stub
  		StringBuilder sqlStr = new StringBuilder();
  		sqlStr.append("SELECT t1.*,t2.USERNAME USER_NAME,t3.SHOP_NAME FROM KT_SHOP_LOSSPROFIT_ITEM t1 FULL JOIN PLATFORM_USER t2 ON t1.CREATE_USER=t2.ID FULL JOIN D_SHOP t3 ON t1.shop_id=t3.shop_id AND T1.ORDER_ID = ? AND T1.STATE = ?");
  		return this.queryAsArray(sqlStr.toString(),new Object[]{orderId,state});
  	}
}
